//
//  ViewController.swift
//  saoma
//
//  Created by ccl on 16/3/14.
//  Copyright © 2016年 CC. All rights reserved.
//

import UIKit
import AVFoundation

class ViewController: UIViewController , AVCaptureMetadataOutputObjectsDelegate {

    var saoma:CCSaoma?;
    // 将CCSaoma界面设置为 控制器的view
    override func loadView() {
        super.loadView();
        saoma = CCSaoma.CreatCCSaoma()
       
        saoma?.frame = view.frame
        view = saoma;
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // 设置默认设置
        saoma!.creatDefult();
        
    }
    // 点击屏幕 开始扫描二维码
    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
        
        saoma?.startSaoma();
        // 开启动画
        saoma?.creatStartQRAnimation();
    }
    
    
    // 设置回话
    func creatSession(){
        if !session.canAddInput(inputDevice){
            return;
        }
        if !session.canAddOutput(outPutDate){
            return;
        }
    // 检测完毕. 给回话添加输入,输出设备
        session.addInput(inputDevice);
        session.addOutput(outPutDate);
        
        // 设置 元数据 检测数据的类型
        outPutDate.metadataObjectTypes = outPutDate.availableMetadataObjectTypes;
        // 设置代理
        outPutDate.setMetadataObjectsDelegate(self, queue:dispatch_get_main_queue());
        
    }
    
    // MARK: 输出代理
    func captureOutput(captureOutput: AVCaptureOutput!, didOutputMetadataObjects metadataObjects: [AnyObject]!, fromConnection connection: AVCaptureConnection!) {
        print(metadataObjects);
    }
    // MARK: 懒加载
   // 回话
    lazy var session:AVCaptureSession = {
         return  AVCaptureSession()
    }()
    // 输入设备
    lazy var inputDevice:AVCaptureDeviceInput? = {
        
        let device = AVCaptureDevice.defaultDeviceWithMediaType(AVMediaTypeVideo);
        do{
            return  try AVCaptureDeviceInput(device: device)
        }catch{
            return nil;
        }
        
    }()
    // 输出设备
    lazy var outPutDate: AVCaptureMetadataOutput = {
    
        return AVCaptureMetadataOutput();
    }()
}

